package com.verykim.video;

import java.util.List;

import com.google.appengine.api.datastore.AsyncDatastoreService;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

public final class KeyValue {

	public static final String P_KEY = "key";
	public static final String UP_VALUE = "value";

	private String key;
	private Object value;

	public KeyValue(final String key, final List<Key> keysOnlyVideos) {
		this.key = key;
		this.value = keysOnlyVideos;
	}

	public static boolean put(final KeyValue kv) {
		final AsyncDatastoreService ads = DatastoreServiceFactory
				.getAsyncDatastoreService();
		try {
			final Entity E = new Entity(KeyValue.class.getSimpleName(), kv.key);
			final Object v = kv.value;
			if (kv.value instanceof String) {
				E.setUnindexedProperty(UP_VALUE, new Text(v.toString()));
			} else {
				E.setUnindexedProperty(UP_VALUE, kv.value);
			}
			ads.put(E).isDone();
		} catch (Exception e) {
			return false;
		} finally {
		}
		return true;
	}

	public static boolean delete(final String key) throws Exception {
		final AsyncDatastoreService ads = DatastoreServiceFactory
				.getAsyncDatastoreService();
		try {
			final Key k = KeyFactory.createKey(KeyValue.class.getSimpleName(),
					key);
			ads.delete(k);
		} catch (Exception e) {
			return false;
		} finally {

		}
		return true;
	}

	public static Entity get(final String key) {
		final DatastoreService ds = DatastoreServiceFactory
				.getDatastoreService();
		try {
			final Key k = KeyFactory.createKey(KeyValue.class.getSimpleName(),
					key);
			return ds.get(k);
		} catch (Exception e) {
			return null;
		} finally {

		}
	}

}
